﻿using System.Linq;
using System.Collections.Generic;

namespace Algoritmia.DataStructures.Collections
{
    public class LinkedListCollection<T> : ICollection<T>
    {
        private readonly LinkedList<T> _list;

        public LinkedListCollection(IEnumerable<T> data=null)
        {
            data = data ?? Enumerable.Empty<T>();
            _list = new LinkedList<T>(data);
        }

        #region ICollection<T> Members

        public void Add(T item)
        {
            _list.AddLast(item);
        }

        public void Clear()
        {
            _list.Clear();
        }

        public bool Contains(T item)
        {
            return _list.Contains(item);
        }

        public bool Remove(T item)
        {
            return _list.Remove(item);
        }

        #endregion

        #region ICountable members

        public int Count
        {
            get { return _list.Count; }
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion
        public override string ToString()
        {
            return string.Format("{0}<{1}>[{2}]", GetType().Name, typeof(T).Name, string.Join(", ", this));
        }
    }
}
